﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

[System.Serializable]
public class Item
{
    public int id;
    public string name;
    public int count;

    public Item(int id, string name, int count)
    {
        this.id = id;
        this.name = name;
        this.count = count;
    }
}

public class UserInfo
{
    public string name;
    public int age;
    public bool sex;
    public float valf;
    public double vald;

    public int[] ids;
    public List<int> ids2;
    public Dictionary<int, string> dic;
    public Dictionary<string, string> dic2;

    public Item item;
    public Item item2;
    public List<Item> items;

    [SerializeField]
    private int priNumVal;
    [SerializeField]
    protected int proNumVal;

    public void SetVal(int priNumVal, int ProNumVal)
    {
        this.priNumVal = priNumVal;
        this.proNumVal = ProNumVal;
    }
}

[System.Serializable]
public class Role
{
    public int id;
    public string name;
    public int age;
}

public class RoleInfo
{
    public List<Role> list;
}

public class Lesson1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string Path = Application.persistentDataPath + "/Test.json";
        #region 知识点一 必备知识点-在文件中存读字符串
        //1.存储字符串到指定路径文件中
        //参数一:存储路径(路径必须存在,不支持动态创建文件夹,没有对应的文件夹会报错)
        //参数二:写入的字符串内容
        File.WriteAllText(Path, "这是一条测试数据!");
        Debug.LogFormat("Path:{0}", Path);
        //2.在指定路径文件中读取字符串 
        string content = File.ReadAllText(Path);
        Debug.LogFormat("content:{0}", content);
        #endregion

        #region 知识点二 使用JsonUtility进行序列
        //序列化:把内存中的数据存储到硬盘上
        //方法:
        //JsonUtility.ToJson(对象)

        UserInfo user = new UserInfo();
        user.name = "zabry";
        user.age = 18;
        user.sex = false;
        user.valf = 12.12f;
        user.vald = 2.31215125;
        user.ids = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        user.ids2 = new List<int>() { 10,20,30,40 };
        user.dic = new Dictionary<int, string>() { { 100,"long time no see"}, { 200, "what you name"}, { 300, "goto bed"} };
        user.dic2 = new Dictionary<string, string> { { "a", "aaa" }, { "b", "bbb" }, { "c", "ccc" } };
        user.item = new Item(10000, "正阳鞋", 1);
        user.item2 = null;
        user.items = new List<Item>() { new Item(100, "十字镐", 1), new Item(101, "木头", 100), new Item(102, "石头", 130) };
        user.SetVal(1111,2222);
        string Path2 = Application.persistentDataPath + "/User.json";
        string userString = JsonUtility.ToJson(user);
        //File.WriteAllText(Path2, userString);

        //注意:
        //1.float序列化时看起来会有一些误差
        //2.自定类需要加上序列化特性[System.Serializable]
        //3.想要序列化私有变量 需要加上特性[SerializeField]
        //4.JsonUtility不支持字典
        //5.JsonUtility存储null对象不会是null 而是默认值的数据
        #endregion

        #region 知识点三 使用JsonUtlity进行反序列化
        //反序列化:把硬盘上的数据 读取到内存中
        //方法:
        //JsonUtility.FromJson
        string userContent = File.ReadAllText(Path2);
        UserInfo user1 = JsonUtility.FromJson(userContent, typeof(UserInfo)) as UserInfo;
        UserInfo user2 = JsonUtility.FromJson<UserInfo>(userContent);
        Debug.LogFormat("user2 name:{0}", user2.name);

        //注意:
        //如果Json中少数据了,读取到内存中类对象时不会报错
        #endregion

        #region 知识四 注意事项
        //1.JsonUtility无法直接读取数据集合
        //string Path3 = Application.persistentDataPath + "/Role.json";
        //string roleContent = File.ReadAllText(Path3);
        //读取报错
        //List<Role> roles = JsonUtility.FromJson<List<Role>>(roleContent);

        //解决方法
        string Path4 = Application.persistentDataPath + "/Role2.json";
        string role2Content = File.ReadAllText(Path4);
        RoleInfo roles2 = JsonUtility.FromJson<RoleInfo>(role2Content);
        Debug.LogFormat("roles2 count:{0}", roles2.list.Count);
        //2.文本编码格式需要UTF-8,不然无法加载
        #endregion
    }
}
